import http from "../http/index.js"
import store from '../../store/index.js'

export const checkVersion = (sVersion, mVersion) => {
	const oldV = Number(mVersion.replace(/\./g, ""));
	const newV = Number(sVersion.replace(/\./g, ""));
	return oldV > newV ? true : false
}

export const update = (version, ifClick) => {
	http.update().then(res => {
		uni.hideLoading()
		const data = res.data.data
		if (!data.version) return;
		if (checkVersion(version, data.version)) {
			store.dispatch('setUpdate', data)
			uni.showModal({
				title: '提示',
				content: '检测到新版本' + data.version,
				confirmText: '马上更新',
				cancelText: '稍后更新',
				success: info => {
					if (info.confirm) {
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/user/update/index'
							});
						},500)
					}
				}
			});
			return;
		}
		if (!ifClick) return
		let content = '当前为最新版本' + version;
		uni.showModal({
			title: '提示',
			content: content,
			showCancel: false,
			success: res => {
				if (res.confirm) {
				}
			}
		});
	})
}